package com.example.administrator.liveapp.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.Toast;

import com.example.administrator.liveapp.ApplicationUtils;
import com.example.administrator.liveapp.R;
import com.example.administrator.liveapp.Xutils3.MyCallBack;
import com.example.administrator.liveapp.Xutils3.XUtils;
import com.example.administrator.liveapp.utils.AppUrl;
import com.example.administrator.liveapp.utils.CheckInfoUtill;
import com.example.administrator.liveapp.utils.MD5Utils;

import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.common.util.LogUtil;

import java.util.HashMap;
import java.util.Map;

/**
 * 忘记密码activity（已经没有用了）
 */

public class ForgotPasswordPage3 extends Activity {

    private Button complete_button;
    private RadioButton login_return;
    private EditText new_password;
    private EditText repeat_password;
    private String phone;//用户手机号
    private String pass;//用户原密码
    private String identifying;//用户验证码
    private String userpassword;//新密码
    private String userrepeat;//重复密码

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.forgot_password_page3);
        ApplicationUtils applicationUtils = new ApplicationUtils();
        applicationUtils.addActivity(this);
        initView();
        setData();
        listener();
    }

    private void setData() {
        Intent intent = getIntent();
        phone = intent.getStringExtra("phone").toString();
        pass = intent.getStringExtra("pass").toString();
        identifying = intent.getStringExtra("identifying").toString();
        complete_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                userpassword = new_password.getText().toString();
                userrepeat = repeat_password.getText().toString();
                String userpasswords = MD5Utils.getMd5Value(userpassword);
                if(CheckInfoUtill.isPasswordNO(userpassword)){
                    if(userpassword.equals(userrepeat)){
                        if(!userpasswords.equals(pass)){
                            String url = AppUrl.URL + AppUrl.ReviseURL;
                            Map<String,String> map=new HashMap<>();
                            map.put("consumerTel", phone);
                            map.put("pass", userpassword);
                            map.put("identifying", identifying);
                            XUtils.Get(url,map,new MyCallBack<String>(){
                                @Override
                                public void onSuccess(String result) {
                                    super.onSuccess(result);
                                    JSONObject jsonObject = null;
                                    try {
                                        jsonObject = new JSONObject(result);
                                        String code = jsonObject.optString("code");
                                        if(code.equals("4600")){
                                            Toast.makeText(getApplicationContext(),"密码修改成功",Toast.LENGTH_SHORT).show();
                                            Intent intent = new Intent();
                                            intent.setClass(ForgotPasswordPage3.this,WxLoginPage.class);
                                            startActivity(intent);
                                            overridePendingTransition(R.anim.zoomin,R.anim.zoomout);
                                        }else{
                                            Toast.makeText(ForgotPasswordPage3.this,"密码修改失败"+code,Toast.LENGTH_SHORT).show();
                                        }

                                    } catch (JSONException e) {
                                        e.printStackTrace();
                                        Toast.makeText(ForgotPasswordPage3.this,"密码修改失败2"+e.getMessage(),Toast.LENGTH_SHORT).show();
                                    }

                                }

                                @Override
                                public void onError(Throwable ex, boolean isOnCallback) {
                                    super.onError(ex, isOnCallback);
                                    LogUtil.e(ex.getMessage()+"6666666666");
                                }
                            });
                        }else{
                            Toast.makeText(getApplicationContext(),"新密码不能与旧密码相同",Toast.LENGTH_SHORT).show();
                        }
                    }else {
                        Toast.makeText(getApplicationContext(),"两次输入的密码不同",Toast.LENGTH_SHORT).show();
                    }
                }else {
                    Toast.makeText(getApplicationContext(),"密码最少6位数,最大16位数",Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    private void initView() {
        complete_button = (Button) findViewById(R.id.complete_button);
        login_return = (RadioButton) findViewById(R.id.login_return);
        new_password = (EditText) findViewById(R.id.new_password);
        repeat_password = (EditText) findViewById(R.id.repeat_password);
    }

    private void listener() {
        login_return.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }
}
